package com.atguigu.eduservice.controller.frontcontroller;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.nacosclass.service_edu.CourseOrderVO;
import com.atguigu.eduservice.client.frontClient.OrdersFrontClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.dto.course.CourseFrontPageDTO;
import com.atguigu.eduservice.entity.vo.chapter.ChapterVo;
import com.atguigu.eduservice.entity.vo.course.CourseFrontVO;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前台前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/eduservice/frontCourse")
@CrossOrigin
@Slf4j
@Api("前台课程信息接口")
public class EduCourseFrontController {
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    EduTeacherService eduTeacherService;
    @Autowired
    private OrdersFrontClient ordersFrontClient;
    //查询前八个热门课程
    @GetMapping("courseFrontIndex")
    public Result courseFrontIndex() {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.orderByDesc("id");
        eduCourseQueryWrapper.last("limit 8");
        List<EduCourse> eduCourseFrontlist = eduCourseService.list(eduCourseQueryWrapper);
        return Result.ok().data("eduCourseFrontlist",eduCourseFrontlist);
    }

    //课程的分页查询
    @PostMapping("getCourseFrontList/{page}/{limit}")
    public Result getFrontCourseList(
            @PathVariable long page,
            @PathVariable long limit,
            @RequestBody(required = false)CourseFrontPageDTO courseFrontPageDTO
            ){

        log.info("掉我赛:{}",courseFrontPageDTO.toString());

        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        Map<String,Object> map = eduCourseService.getCourseList(eduCoursePage,courseFrontPageDTO);

        log.info("掉我没有map:{}",map);
        return Result.ok().data("map",map);
    }
    //课程的详情方法
    @PostMapping("getCourseFrontInfo/{courseId}")
    public Result getCourseFrontInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据id查询详细信息
        CourseFrontVO courseFrontVO = eduCourseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节小节id
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideoByCourseId(courseId);
        log.info("courseId:{}，request:{}",courseId,JwtUtils.getMemberIdByJwtToken(request));
        boolean buyCourse = false;
        if(JwtUtils.getMemberIdByJwtToken(request) == null){
             buyCourse = ordersFrontClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        }

        return Result.ok().data("courseFrontVO",courseFrontVO).data("chapterVoList",chapterVoList).data("buyCourse",buyCourse);
    }

    //订单接口：根据课程id返回订单需要对应的信息
    @GetMapping("orderInfo/{courseId}")
    public CourseOrderVO orderInfo(@PathVariable String courseId){
        EduCourse course = eduCourseService.getById(courseId);
        EduTeacher teacher = eduTeacherService.getById(course.getTeacherId());
        CourseOrderVO courseOrderVO = new CourseOrderVO();
        courseOrderVO.setTeacherName(teacher.getName());
        BeanUtils.copyProperties(course,courseOrderVO);
        log.info("我是订单对象:{}",courseOrderVO);
        return courseOrderVO;
    }




















}
